package com.ita.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ita.dao.MerchantDao;
import com.ita.pojo.Merchant;
import com.ita.util.DBUtil;

public class MerchantDaoImpl implements MerchantDao {

	private Logger logger = Logger.getLogger(MerchantDaoImpl.class);

	@Override
	public int addMerchant(Merchant merchant) {
		Connection connection = null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			String sql = "insert into m_user values(?,?,?,?,?,?,?,?,?,?,?)";
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, merchant.getId());
			pst.setString(2, merchant.getName());
			pst.setString(3, merchant.getStoreName());
			pst.setString(4, merchant.getPassword());
			pst.setString(5, merchant.getIdcard());
			pst.setString(6, merchant.getImg());
			pst.setString(7, merchant.getAddress());
			pst.setInt(8, merchant.getState());
			pst.setInt(9, merchant.getBlock());
			pst.setLong(10, merchant.getSubTime());
			pst.setLong(11, merchant.getVerifyTime());
			m = pst.executeUpdate();
			return m;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return m;
		} finally {
			DBUtil.close(connection, pst, null);
		}

	}

	@Override
	public List<Merchant> findAll() {
		String sql = "select * from m_user";
		Connection connection = null;
		PreparedStatement pst = null;
		Merchant merchant = null;
		List<Merchant> list = new ArrayList<Merchant>();
		ResultSet rs = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				merchant = new Merchant();
				merchant.setId(rs.getString("id"));
				merchant.setName(rs.getString("name"));
				merchant.setImg(rs.getString("img"));
				merchant.setStoreName(rs.getString("storename"));
				merchant.setIdcard(rs.getString("idcard"));
				merchant.setAddress(rs.getString("address"));
				merchant.setState(rs.getInt("state"));
				merchant.setBlock(rs.getInt("block"));
				merchant.setPassword(rs.getString("pwd"));
				merchant.setVerifyTime(rs.getLong("verifyTime"));
				merchant.setSubTime(rs.getLong("subTime"));
				list.add(merchant);
			}
			return list;
		} catch (SQLException e) {
			logger.equals(e.getMessage());
			return list;
		} finally {
			DBUtil.close(connection, pst, rs);
		}
	}

	@Override
	public int updateBlock(String id, Integer block) {
		String sql = "update m_user set block=? where id =?";
		Connection connection = null;
		PreparedStatement pst = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setInt(1, block);
			pst.setString(2, id);
			pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			return 0;
		} finally {
			DBUtil.close(connection, pst, null);

		}
	}

	@Override
	public Merchant loadMerchant(String name) {
		String sql = "select * from m_user where name=?";
		Connection connection = null;
		PreparedStatement pst = null;
		Merchant merchant = null;
		ResultSet rs = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, name);
			rs = pst.executeQuery();
			while (rs.next()) {
				merchant = new Merchant();
				merchant.setId(rs.getString("id"));
				merchant.setName(rs.getString("name"));
				merchant.setImg(rs.getString("img"));
				merchant.setStoreName(rs.getString("storename"));
				merchant.setIdcard(rs.getString("idcard"));
				merchant.setAddress(rs.getString("address"));
				merchant.setState(rs.getInt("state"));
				merchant.setBlock(rs.getInt("block"));
				merchant.setPassword(rs.getString("pwd"));
				merchant.setVerifyTime(rs.getLong("verifyTime"));
				merchant.setSubTime(rs.getLong("subTime"));
			}
			return merchant;
		} catch (SQLException e) {
			logger.equals(e.getMessage());
			return merchant;
		} finally {
			DBUtil.close(connection, pst, rs);
		}
	}

	@Override
	public int getBlock(String id) {

		String sql = "select block from m_user where id=?";
		Connection connection = null;
		PreparedStatement pst = null;
		int block = 0;
		ResultSet rs = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, id);
			rs = pst.executeQuery();
			return rs.getInt("block");
		} catch (SQLException e) {
			logger.equals(e.getMessage());
			return block;
		} finally {
			DBUtil.close(connection, pst, rs);
		}
	}

	@Override
	public int updateMer(Merchant merchant) {
		String sql = "update m_user set name=?, state=?, block=?, subTime=?, verifyTime=? where id=?";
		Connection connection = null;
		PreparedStatement pst = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, merchant.getName());
			pst.setInt(2, merchant.getState());
			pst.setInt(3, merchant.getBlock());
			pst.setLong(4, merchant.getSubTime());
			pst.setLong(5, merchant.getVerifyTime());
			pst.setString(6, merchant.getId());
			pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			return 0;
		} finally {
			DBUtil.close(connection, pst, null);

		}
	}
	
	@Override
	public int updateMerchant(Merchant merchant) {
		String sql = "update m_user set name=?, storeName=?, pwd=?, idcard=?, img=?, address=?, state=?, block=?, subTime=?, verifyTime=? where id=?";
		Connection connection = null;
		PreparedStatement pst = null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, merchant.getName());
			pst.setString(2, merchant.getStoreName());
			pst.setString(3, merchant.getPassword());
			pst.setString(4, merchant.getIdcard());
			pst.setString(5, merchant.getImg());
			pst.setString(6, merchant.getAddress());
			pst.setInt(7, merchant.getState());
			pst.setInt(8, merchant.getBlock());
			pst.setLong(9, merchant.getSubTime());
			pst.setLong(10, merchant.getVerifyTime());
			pst.setString(11, merchant.getId());
			pst.executeUpdate();
			return 1;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			return 0;
		} finally {
			DBUtil.close(connection, pst, null);

		}
	}

	@Override
	public int checkPassword(String id, String password) {
		// TODO Auto-generated method stub
		String sql = "select count(*) count1 from m_user where id=? and pwd=?";
		Connection connection = null;
		PreparedStatement pst = null;
		int result=0;
		ResultSet rs=null;
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, id);
			pst.setString(2, password);
			rs=pst.executeQuery();
			result=Integer.parseInt(rs.getString(rs.getString("count1")));
			return result;
		} catch (SQLException e) {
			logger.equals(e.getMessage());
			return result;
		} finally {
			DBUtil.close(connection, pst, rs);
		}
	}

}
